//
//  FuJinMoreViewController.m
//  GOGOTravel
//
//  Created by dllo on 16/3/6.
//  Copyright © 2016年 果果. All rights reserved.
//

#import "FuJinMoreViewController.h"
#import "fujinMore.h"

#import "FuJinMoreERCollectionViewCell.h"
#import "FuJin.h"
#import "SmTableViewCell.h"
@interface FuJinMoreViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) NSMutableArray *dataModel;
@property (nonatomic, retain) BaseTableView *tableView;

@property (nonatomic, assign) NSInteger page;
@property (nonatomic, assign) BOOL isHearder;
@end

@implementation FuJinMoreViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}

- (void)dealloc{
    [_dataModel release];
    [_tableView release];
    
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0.9 green:0.29 blue:0.44 alpha:1];
    self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
    self.navigationController.navigationBar.translucent = NO;
    self.view.backgroundColor = [UIColor redColor];
   
    self.dataModel = [NSMutableArray array];
    
   
    
    _page = 0;
    [self createTableView];
    
    [self handleData];
    
    [self addHeaderRef];
    
    [self addFooter];
}

- (void)setModel:(FuJin *)model{
    if (_model != model) {
        [_model release];
        _model = [model retain];
    }
    
}

- (void)addHeaderRef{
    
    [_tableView addHeaderWithCallback:^{
        
        _page = 0;
        
        _isHearder = YES;
        
        [self handleData];
        
    }];
    
    
    
}

- (void)addFooter{
    
    [_tableView addFooterWithCallback:^{
       
        _page = _page + 5;
        
        _isHearder = NO;
        
        [self handleData];
    }];
    
    
    
}

- (void)createTableView{
    
    self.tableView = [[BaseTableView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height-64)];
    
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    
    [_tableView release];
    
    [_tableView registerClass:[SmTableViewCell class] forCellReuseIdentifier:@"SmTableViewCell"];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataModel.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
   fujinMore *model = _dataModel[indexPath.row];
    
    NSString *str = model.trip_text;
    
    NSDictionary *att = @{NSFontAttributeName:[UIFont systemFontOfSize:17]};
    
 
    
    CGRect rect = [str boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width - 20, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:att context:nil];
    
    
    return rect.size.height + 60 + 150;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    SmTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SmTableViewCell"];
    cell.backgroundColor = [UIColor whiteColor];
    fujinMore *model = _dataModel[indexPath.row];
    cell.model = model;
    return cell;
}



- (void)handleData {
    
    
    
    NSNumber *type = _model.type;
    NSNumber *idStr = _model.id;
    
    NSString *str = [NSString stringWithFormat:@"http://api.breadtrip.com/destination/place/%@/%@/poi_trips/?start=%ld&count=5", type, idStr, (long)_page];
    
    
    [AFNetWorkingHandle GETRequestWithURLString:str completionWithSuccess:^(id result) {
        NSDictionary *dic = result;
        
       
        
        if (_isHearder == YES) {
            [_dataModel removeAllObjects];
        }
        
        NSArray *arr = dic[@"trips"];
        
        NSArray *arr1 = [NSArray array];
        arr1 = [fujinMore ModelHandleWithArray:arr];
        
        for (fujinMore *model in arr1) {
            [_dataModel addObject:model];
        }
        
        
        [_tableView reloadData];
        [_tableView headerEndRefreshing];
        [_tableView footerEndRefreshing];
        
       
        
        //如果数据为空的提示图
        if (_dataModel.count == 0) {
            
            UIView *view = [[UIView alloc] initWithFrame:self.view.bounds];
            view.backgroundColor = [UIColor whiteColor];
            view.tag = 1000;
            [self.view addSubview:view];
            [view release];
            
            UIImageView *image = [[UIImageView alloc] init];
            image.frame = CGRectMake(0, 0, 50, 50);
            image.tag = 2000;
            image.center = view.center;
            image.image = [UIImage imageNamed:@"iconfont-yewuyeicon05.png"];
            
            [view addSubview:image];
            [image release];
        }
        
        
        
        
    } withFailure:^(id error) {
        
    }];
    
    
    
    
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
